package com.bwie.mq;

import com.bwie.mapper.SmsMapper;
import com.bwie.pojo.po.Sms;
import com.bwie.pojo.po.User;
import com.bwie.utils.SendMsgUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.utils.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class OrderParkConsumer implements MessageListener {
    @Autowired
    private SmsMapper smsMapper;
    @Override
    public void onMessage(Message message) {
        byte[] body = message.getBody();
        //反序列化
        User user = (User) SerializationUtils.deserialize(body);
        System.out.println(user.toString());
        String sendRemarks="尊敬的"+user.getUsername()+"先生/女士，您的车位预定成功。";
        //阿里云发送短信
        String isOk = SendMsgUtil.sendSms(user.getUserPhone(), "1111");
        if (!"OK".equals(isOk)){
            //发送失败重新发送
            isOk = SendMsgUtil.sendSms(user.getUserPhone(), "1111");
        }

        //解析发送结果，保存到消息发送记录表中。
        Sms sms = new Sms();
        sms.setSmsRemarks(sendRemarks);
        sms.setSmsTime(new Date());
        sms.setSmsPhone(user.getUserPhone());
        sms.setSmsResp(isOk);

        smsMapper.addSms1(sms);

    }
}
